package main

import (
	"github.com/ying32/govcl/vcl"
	"github.com/ying32/govcl/vcl/types"
)

func (f *MainForm) coderMode(callback func(i int32)) {
	var modeIndex int32 = -1

	mode := vcl.NewForm(f)
	mode.SetPosition(types.PoMainFormCenter)
	mode.SetWidth(width * 1.5)
	mode.SetHeight(height * 1.5)
	mode.SetCaption("选择生成模式")

	// 顶部菜单
	menu := vcl.NewMainMenu(mode)
	item := vcl.NewMenuItem(mode)
	item.SetCaption("选中当前并退出(&C)")
	item.SetOnClick(func(sender vcl.IObject) {
		callback(modeIndex)
		mode.Close()
	})
	menu.Items().Add(item)

	// 左侧菜单
	sidebar := vcl.NewListBox(mode)
	sidebar.SetParent(mode)
	sidebar.SetStyle(types.LbOwnerDrawFixed)
	sidebar.SetBorderStyle(types.BsNone)
	sidebar.SetAlign(types.AlLeft)
	sidebar.SetWidth(modeSidebarWidth)
	sidebar.SetColor(sidebarColor)
	sidebar.SetItemHeight(sidebarItemHeight)
	sidebar.SetOnDrawItem(func(control vcl.IWinControl, index int32, aRect types.TRect, state types.TOwnerDrawState) {
		canvas := sidebar.Canvas()
		menuItemText := sidebar.Items().Strings(index)
		textHeight := canvas.TextHeight(menuItemText)
		canvas.Font().SetColor(sidebarFontColor)
		canvas.FillRect(aRect)
		canvas.Pen().SetColor(sidebarColor)
		canvas.Rectangle(aRect.Left, aRect.Top, aRect.Right, aRect.Bottom)
		if state.In(types.OdSelected) {
			canvas.Brush().SetColor(sidebarSelectedColor)
			canvas.Rectangle(aRect.Left, aRect.Top, aRect.Right, aRect.Bottom)
			canvas.Font().SetColor(sidebarFontColor)
			if state.In(types.OdFocused) {
				canvas.DrawFocusRect(aRect)
			}
		}
		canvas.TextOut(modeSidebarItemLeft, aRect.Top+(sidebarItemHeight-textHeight)/2, menuItemText)
	})
	for _, mode := range coderModes {
		sidebar.Items().Add(mode)
	}
	// 选择左侧菜单，主框架内容变化
	sidebar.SetOnClick(func(sender vcl.IObject) {
		modeIndex = sidebar.ItemIndex()
		// 主框架内容
		content := vcl.NewPageControl(mode)
		content.SetParent(mode)
		content.SetAlign(types.AlClient)
		content.SetOnChange(func(sender vcl.IObject) {
		})
		titles := []string{"controller.tpl", "service.tpl", "entity.tpl", "mapper.tpl", "mapper_xml.tpl", "vue.tpl", "vue_api.tpl"}
		var temp = make([]string, 0)
		if modeIndex == 0 {
			temp = append(temp, []string{nlController, nlService, nlEntity, nlMapper, nlMapperXml, nlVue, nlVueApi}...)
		} else if modeIndex == 1 {
			temp = append(temp, []string{commonController, commonService, commonEntity, commonMapper, commonMapperXml, commonVue, commonVueApi}...)
		} else if modeIndex == 2 {
			titles = []string{}
		} else if modeIndex == 3 {
			titles = []string{"entity.tpl", "mapper.tpl", "mapper_xml.tpl"}
			temp = append(temp, []string{mybatisPlusEntity, mybatisPlusMapper, mybatisPlusMapperXml}...)
		} else {
			titles = []string{}
		}
		for i := 0; i < len(titles); i++ {
			title := vcl.NewTabSheet(content)
			title.SetPageControl(content)
			title.SetCaption(titles[i])
			tpl := vcl.NewMemo(content)
			tpl.SetParent(title)
			tpl.SetAlign(types.AlClient)
			tpl.SetScrollBars(types.SsAutoBoth)
			tpl.SetReadOnly(true)
			tpl.SetText(temp[i])
		}
	})

	mode.ShowModal()
}
